package com.hd.trans.widgets

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.hd.trans.R
import com.hd.trans.files.adapter.BaseAdapter
import com.hd.trans.files.bean.Album
import com.hd.trans.files.bean.MediaOptions
import com.hd.trans.files.holder.BaseViewHolder

/**
 * @author       qianjujun
 * @email        qianjujun@163.com
 * @createTime  2021/1/5 20:33
 * @describe
 */
class AlbumAdapter(private val mediaOptions: MediaOptions) :
    RecyclerView.Adapter<BaseViewHolder>() {
    private var selectPosition = 0

    private var list = ArrayList<Album>()

    private var itemClickListener: BaseAdapter.OnItemChildClickListener<Album>? = null


    fun selectItem(dataPosition: Int) {
        val lastPosition = selectPosition
        selectPosition = dataPosition
        notifyItemChanged(lastPosition)
        notifyItemChanged(selectPosition)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseViewHolder {
        var holder = BaseViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.album_adapter, parent, false)
        )
        holder.itemView.setOnClickListener {
            itemClickListener?.onItemChildClick(
                holder.itemView,
                list[holder.layoutPosition],
                holder.layoutPosition
            )
        }
        return holder

    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        var data = list[position]
        Glide.with(holder.itemView.context).load(data.getmCoverUri())
            .into(holder.getView(R.id.iv_image))
        holder.setText(R.id.tv_text, data.getmDisplayName())
        holder.setText(
            R.id.tv_num, String.format(
                holder.itemView.context.getString(
                    if (mediaOptions.mediaType == MediaOptions.TYPE_VIDEO) R.string.trans_album_video_num
                    else R.string.trans_album_image_num
                ),
                data.getmCount()
            )
        )
        holder.getView<ImageView>(R.id.iv_check).visibility =
            if (selectPosition == position) View.VISIBLE else View.INVISIBLE

    }

    override fun getItemCount(): Int {
        return list.size
    }

    fun setList(list: List<Album>) {
        this.list.addAll(list)
    }

    fun setOnModuleItemClickListener(listener: BaseAdapter.OnItemChildClickListener<Album>) {
        this.itemClickListener = listener
    }

}